﻿Imports System.Windows.Forms
Imports TBCC.Utility

Public Class UserManageForm

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub UserManageForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.DataGridView1.AutoGenerateColumns = False
        LoadUsers()
        LoadRoles()

        Logger.Write(User.Instance.uID, User.Instance.uName, "查看用户列表", tbccLogOperateType.UserManage)
    End Sub

    ''' <summary>
    ''' 加载所有用户
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadUsers()
        Dim dt As DataTable
        dt = UserClass.GetUserList()
        Me.DataGridView1.DataSource = dt
    End Sub

    Private Sub LoadRoles()
        Dim dt As DataTable
        dt = RoleClass.GetRoles()
        For Each r As DataRow In dt.Rows

        Next
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim frm As New UserEditForm()
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim msg As String = String.Format("增加用户{0}({1})信息", frm.UserIDTextBox.Text, frm.RealNameTextBox.Text)
            Logger.Write(User.Instance.uID, User.Instance.uName, msg, tbccLogOperateType.UserManage)
            Me.LoadUsers()
        End If
    End Sub

    Private Sub btnModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModify.Click
        If Me.DataGridView1.CurrentRow Is Nothing Then
            Exit Sub
        End If

        Dim ui As New UserItem

        With Me.DataGridView1.CurrentRow
            ui.uID = .Cells("uID").Value
            ui.uName = .Cells("uName").Value
            ui.roleID = .Cells("roleID").Value
            ui.roleName = .Cells("roleName").Value
        End With

        Dim frm As New UserEditForm(ui)
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim msg As String = String.Format("修改用户{0}({1})信息", frm.UserIDTextBox.Text, frm.RealNameTextBox.Text)
            Logger.Write(User.Instance.uID, User.Instance.uName, msg, tbccLogOperateType.UserManage)
            Me.LoadUsers()
        End If
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If Me.DataGridView1.CurrentRow Is Nothing Then
            Exit Sub
        End If

        Dim uID As String
        uID = Me.DataGridView1.CurrentRow.Cells("uID").Value

        Dim uName As String
        uName = Me.DataGridView1.CurrentRow.Cells("uName").Value

        Dim tip As String
        tip = String.Format("是否要删除【{0}】？", uName)

        If msg.ShowYesNo(tip) = Windows.Forms.DialogResult.Yes Then
            Dim ret As Integer
            ret = UserClass.Delete(uID)

            If ret = 0 Then
                Dim msg As String = String.Format("删除用户{0}({1})", uID, uName)
                Logger.Write(User.Instance.uID, User.Instance.uName, msg, tbccLogOperateType.UserManage)

                LoadUsers()
            Else
                msg.ShowError("删除失败")
            End If
        End If

    End Sub

    Private Sub DataGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
        Me.btnModify.PerformClick()
    End Sub


End Class
